<template>
  <el-dialog
      v-model="dialogVisible"
      width="780"
  >
    <template #header>
      <div class="my-header">
        信息保存
      </div>
    </template>
    <div class="personDetail">
      <div class="personDetail-title">
        <div class="personDetail-title-li" :class="{action:navIndex===index,action1:navIndex===index&&navIndex==1}"
             @click="changeNav(index)" v-for="(item,index) in navData"
             :key="index">{{ item.lable }}
        </div>
      </div>
      <div style="padding: 0 10px 10px" v-if="navIndex==0">
        <div>
          <div style="position: relative">
            <el-divider content-position="left">
              <div class="base-info">基本信息</div>
            </el-divider>
            <div class="tips-pic-box" @click="hiddenRow(1)">
              <img class="tips-pic" :src="show1?tipsUp:tipsDown">
            </div>
          </div>
          <div class="content-box" v-if="show1">
            <ul class="content-box-left" :class="{'content-box-left3':!show2}">
              <li>
                <div>姓名：</div>
                <div class="name">
                  <el-input v-model="formData.name" style="width: 240px" placeholder="姓名" />
                </div>
              </li>
              <li>
                <div>性别：</div>
                <div class="name1">
                  <el-select
                    v-model="formData.gender"
                    placeholder="性别"
                    style="width: 240px"
                  >
                    <el-option value="男">男</el-option>
                    <el-option value="女">女</el-option>
                  </el-select>
                </div>
              </li>
              <li>
                <div>民族：</div>
                <div class="name1">
                  <el-input v-model="formData.nation" style="width: 240px" placeholder="民族" />
                </div>
              </li>
              <li>
                <div>出生日期：</div>
                <div class="name1">
                  <el-date-picker
                    v-model="formData.birthDay"
                    type="date"
                    placeholder="出生日期"
                    format="YYYY年MM月DD日"
                    value-format="YYYY年MM月DD日"
                    style="width: 240px"
                  />
                </div>
              </li>
              <li>
                <div>身份证号：</div>
                <div class="name1">
                  <el-input v-model="formData.idCardNo" style="width: 240px" placeholder="身份证号" maxlength="18" />
                </div>
              </li>
            </ul>
            <div class="photo">
              <h5>医生照片</h5>
              <input type="file" ref="fileInput" @change="handleFileUpload" class="hidden-file-input" />
              <div class="pic-3" @click="triggerFileInput" v-if="formData.avatar">
                <img class="pic1" :src="formData.avatar" alt="医生照片">
              </div>
              <div class="pic-3" @click="triggerFileInput" v-else>
                <p style="margin-top: 85px;">点击上传医生照片</p>
              </div>
            </div>
          </div>
        </div>
        <div>
          <div style="position: relative">
            <el-divider content-position="left">
              <div class="base-info">职业信息</div>
            </el-divider>
            <div class="tips-pic-box" @click="hiddenRow(2)">
              <img class="tips-pic" :src="show2?tipsUp:tipsDown">
            </div>
          </div>
          <div class="content-box" v-if="show2">
            <div style="width: 100%">
              <ul class="content-box-left">
                <li>
                  <div class="name-label">执业机构地址：</div>
                  <div class="name2">
                    <el-input v-model="formData.orgAddress" style="width: 300px" placeholder="执业机构地址" />
                  </div>
                </li>
                <li>
                  <div class="name-label">主要执业机构：</div>
                  <div class="name2">
                    <el-input v-model="formData.orgName" style="width: 300px" placeholder="主要执业机构" />
                  </div>
                </li>
              </ul>
              <ul class="content-box-left content-box-left1">
                <li>
                  <div class="name-label">医师级别：</div>
                  <div class="name2">
                    <el-input v-model="formData.doctorLevel" style="width: 220px;;" placeholder="医师级别" />
                  </div>
                </li>
                <li>
                  <div class="name-label">执业范围：</div>
                  <div class="name2">
                    <el-input v-model="formData.doctorScope" style="width: 220px;;" placeholder="执业范围" />
                  </div>
                </li>
                <li>
                  <div class="name-label">医师类别：</div>
                  <div class="name2">
                    <el-input v-model="formData.doctorType" style="width: 220px;;" placeholder="医师类别" />
                  </div>
                </li>
                <li>
                  <div class="name-label">毕业院校：</div>
                  <div class="name2">
                    <el-input v-model="formData.school" style="width: 220px;;" placeholder="毕业院校" />
                  </div>
                </li>
                <li>
                  <div class="name-label">任职资格：</div>
                  <div class="name2">
                    <el-input v-model="formData.postQualification" style="width: 220px;;" placeholder="任职资格" />
                  </div>
                </li>
                <li>
                  <div class="name-label">专业：</div>
                  <div class="name2">
                    <el-input v-model="formData.profession" style="width: 220px;;" placeholder="专业" />
                  </div>
                </li>
                <li>
                  <div class="name-label">资格证书编码：</div>
                  <div class="name2">
                    <el-input v-model="formData.certNo" style="width: 220px;;" placeholder="资格证书编码" />
                  </div>
                </li>
                <li>
                  <div class="name-label">学历：</div>
                  <div class="name2">
                    <el-input v-model="formData.education" style="width: 220px;;" placeholder="学历" />
                  </div>
                </li>
                <li>
                  <div class="name-label">执业证书编码：</div>
                  <div class="name2">
                    <el-input v-model="formData.licenseNo" style="width: 220px;;" placeholder="执业证书编码" />
                  </div>
                </li>
                <li>
                  <div class="name-label">审批日期：</div>
                  <div class="name2">
                    <el-date-picker
                      v-model="formData.approvalDate"
                      type="datetime"
                      placeholder="审批日期"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                      style="width: 220px"
                    />
                  </div>
                </li>
                <li>
                  <div class="name-label">审批机关：</div>
                  <div class="name2">
                    <el-input v-model="formData.approvalAgency" style="width: 240px" placeholder="审批机关" />
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      <div v-else style="padding: 0 10px;">
        <div>
          <div style="position: relative">
            <el-divider content-position="left">
              <div class="base-info">该医师请求变更的内容</div>
            </el-divider>
            <div class="tips-pic-box" @click="hiddenRow(3)">
              <img class="tips-pic" :src="show3?tipsUp:tipsDown">
            </div>
          </div>
          <div class="content-box" style="min-height: 576px " v-if="show3">
            <ul class="content-box-left content-box-left2">
              <li>
                <div>毕业学校请求变更为：</div>
              </li>
              <li>
                <div>任职资格请求变更为：</div>
              </li>
              <li>
                <div>学历请求变更为：</div>
              </li>
            </ul>
            <div class="photo photo-1">
              <h5>医师上传的个人照片</h5>
              <div class="pic-3 pic-4">
                照片
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <template #footer>
      <el-button type="primary" size="small" @click="handleSubmit">提交</el-button>
    </template>
  </el-dialog>
</template>

<script lang="js" setup>
import {ref, defineEmits} from "vue";
import {ElDivider, ElInput, ElMessageBox} from "element-plus";
import tipsUp from '../../../assets/tips-up.png'
import tipsDown from '../../../assets/tips-down.png'
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat);

const emit = defineEmits(['reload'])

let dialogVisible = ref(false)
let show1 = ref(true)
let show2 = ref(true)
let show3 = ref(true)
let navIndex = ref(0)
let navData = ref([
      {
        lable: '执业信息',
      }
    ]
)
// 导航栏切换
const changeNav = (index) => {
  navIndex.value = index
}
// 隐藏
const hiddenRow = (type) => {
  if (type == 2) {
    show2.value = !show2.value
  }
  if (type == 1) {
    show1.value = !show1.value
  }
  if (type == 3) {
    show3.value = !show3.value
  }
}

let formData = ref({})
const fileInput = ref(null);

const openInit = (row) => {
  formData.value = {}
  if (row) {
    formData.value = row
  }
  dialogVisible.value = true
}

// 触发文件选择
const triggerFileInput = () => {
  fileInput.value.click();
};

// 处理头像上传
const handleFileUpload = (event) => {
  const file = event.target.files[0];
  if (file) {
    const reader = new FileReader();
    reader.onload = (e) => {
      formData.value.avatar = e.target.result; // Base64 字符串
    };
    reader.readAsDataURL(file);
  }
};

// 计算年龄
const calculateAge = (birthDate) => {
  if (!birthDate) {
    return null;
  }
  const today = new Date();
  const birthDateObj = dayjs(birthDate, 'YYYY年MM月DD日').toDate()
  console.log(birthDateObj)
  let age = today.getFullYear() - birthDateObj.getFullYear();
  const monthDiff = today.getMonth() - birthDateObj.getMonth();
  if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDateObj.getDate())) {
    age--;
  }
  return age;
};

const handleSubmit = async () => {
  formData.value.age = calculateAge(formData.value.birthDay);
  console.log(formData.value.age)
  if (formData.value.id) {
    formData.value.state = "已激活"
    formData.value.isModified = "否"
    formData.value.eCertState = "已申领"
    formData.value.isYtbRegisted = "未医通办注册"

    window.api.UpdateUser(JSON.stringify(formData.value)).then(() => {
      dialogVisible.value = false
      emit('reload')
    });
  } else {
    formData.value.state = "已激活"
    formData.value.isModified = "否"
    formData.value.eCertState = "已申领"
    formData.value.isYtbRegisted = "未医通办注册"
    window.api.InsertUser(JSON.stringify(formData.value)).then(() => {
      dialogVisible.value = false
      emit('reload')
    });
  }
}

defineExpose({openInit})

</script>

<style scoped lang="scss">
.my-header {
  background: url("../../../assets/18.png") no-repeat left center;
  background-size: contain;
  padding-left: 20px;
}

.personDetail {
  border-left: 1px solid #dcdfe6;
  border-right: 1px solid #dcdfe6;
  border-bottom: 1px solid #dcdfe6;
  //padding: 10px;
  padding-top: 0;
  padding-bottom: 10px;

  .personDetail-title {
    border-bottom: 1px solid #dcdfe6;
    display: flex;
    font-size: 13px;

    .personDetail-title-li {
      padding: 5px 15px;
      cursor: pointer;
      position: relative;
      top: 1px;
      z-index: 1;
    }
  }
}


.base-info {
  font-size: 13px;
}

.content-box {
  display: flex;
  padding: 20px 10px 10px;
  border: solid 1px #dcdfe6;
  border-top: 0;

}

.content-box-left {
  flex: 1;

  li {
    padding-bottom: 10px;
    border-bottom: solid 1px #d2cbcb;
    display: flex;
    align-items: center;
    padding-top: 10px;
    font-size: 14px;

    &:nth-child(1) {
      padding-top: 0;
    }

    &:last-child {
      padding-bottom: 0;
      border-bottom: 0;
    }

    .name {
      font-size: 20px;
      color: #1b0ec5;
      margin-left: 30px;
    }

    .name1 {
      font-size: 14px;
      color: #1b0ec5;
      margin-left: 30px;
    }

    .name-label {
      width: 100px;
    }

    .name2 {
      font-size: 14px;
      color: #1b0ec5;
      margin: 0 10px;
    }
  }
}

:deep(.el-divider) {
  margin-bottom: 0;
}

.content-box-left1 {
  display: flex;
  border-top: solid 1px #d2cbcb;
  margin-top: 10px;
  flex-wrap: wrap;

  li {
    width: 49.8%;

    &:nth-child(even) {
      border-left: solid 1px #d2cbcb;
      padding-left: 5px;
      box-sizing: border-box;
    }

    &:first-child {
      padding-top: 10px;
    }

    &:last-child {
      border-bottom: solid 1px #d2cbcb;
      padding-bottom: 10px;
      width: 100%;
    }
  }
}

.content-box-left2 {
  li {
    padding-bottom: 40px;

    &:last-child {
      padding-bottom: 40px;
      border-bottom: solid 1px #d2cbcb;
    }
  }
}

.content-box-left3 {
  li {
    padding-bottom: 40px;

    &:last-child {
      padding-bottom: 40px;

    }
  }
}

.action {
  border-top: solid 1px #d2cbcb;
  //border-left: solid 1px #d2cbcb;
  border-right: solid 1px #d2cbcb;
  background: #f0f0f0;
  color: #333333;
  font-weight: bold;
}

.action1 {
  border-left: solid 1px #d2cbcb
}

.photo {
  width: 180px;
  margin-left: 10px;

  h5 {
    font-weight: normal;
    margin-bottom: 5px;
  }
}

.pic-3 {
  border: solid 1px #d2cbcb;
  height: 80%;
  background: #fff;
  display: flex;
  justify-content: center;

  .pic1 {
    width: 100%;
  }
}

.photo-1 {
  width: 320px;
}

.tips-pic {
  height: 12px;
}

.tips-pic-box {
  position: absolute;
  right: 35px;
  top: -12px;
  z-index: 3;
  background: #f0f0f0;
  padding: 0 3px;
  cursor: pointer;
}

.pic-4 {
  height: 96%;
  align-items: center;
}

.hidden-file-input {
  display: none;
}

</style>
<style>
.el-dialog {
  background: #f0f0f0 !important;

}

.el-tabs__content {
  display: none;
}
</style>
